home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / cujaug93.zip / 1108088A < prev    next >
Text File  |  1993-06-08  |  13KB  |  563 lines

  1.      /*******************************************
  2.      *
  3.      *   edm(..
  4.      *
  5.      *   This function calculates the Euclidean
  6.      *   distance measure for objects in an image.
  7.      *   It calculates the distance from any
  8.      *   pixel=value to the nearest zero pixel
  9.      *
  10.      *******************************************/
  11.  
  12.  
  13. edm(in_name, out_name, the_image, out_image,
  14.           il, ie, ll, le, value)
  15.    char   in_name[], out_name[];
  16.    int    il, ie, ll, le;
  17.    short  the_image[ROWS][COLS],
  18.           out_image[ROWS][COLS],
  19.           value;
  20. {
  21.    int    a, b, count, i, j, k;
  22.    int    length, width;
  23.    struct tiff_header_struct image_header;
  24.  
  25.    if(does_not_exist(out_name)){
  26.       printf("\n\n output file does not exist %s", out_name);
  27.       read_tiff_header(in_name, &image_header);
  28.       round_off_image_size(&image_header,
  29.                            &length, &width);
  30.       image_header.image_length = length*ROWS;
  31.       image_header.image_width  = width*COLS;
  32.       create_allocate_tiff_file(out_name, &image_header,
  33.                                 out_image);
  34.    }  /* ends if does_not_exist */
  35.  
  36.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  37.  
  38.    for(i=0; i<ROWS; i++)
  39.       for(j=0; j<COLS; j++)
  40.          out_image[i][j] = 0;
  41.  
  42.       /***************************
  43.       *
  44.       *   Loop over image array
  45.       *
  46.       ****************************/
  47.  
  48.    printf("\n");
  49.    for(i=0; i<ROWS; i++){
  50.       if( (i%10) == 0) printf("%3d", i);
  51.       for(j=0; j<COLS; j++){
  52.          if(the_image[i][j] == value)
  53.             out_image[i][j] = distance_8(the_image, 
  54.                                          i, j, value);
  55.       }  /* ends loop over j */
  56.    }  /* ends loop over i */
  57.  
  58.    write_array_into_tiff_image(out_name, out_image,
  59.                                il, ie, ll, le);
  60.  
  61. }  /* ends edm */
  62.  
  63.  
  64.  
  65.      /*******************************************
  66.      *
  67.      *   distance_8(..
  68.      *
  69.      *   This function finds the distance from
  70.      *   a pixel to the nearest zero pixel.
  71.      *   It search in all eight directions.
  72.      *
  73.      *******************************************/
  74.  
  75. distance_8(the_image, a, b, value)
  76.    int   a, b;
  77.    short the_image[ROWS][COLS], value;
  78. {
  79.    int i, j, measuring;
  80.    short dist1  = 0,
  81.          dist2  = 0,
  82.          dist3  = 0,
  83.          dist4  = 0,
  84.          dist5  = 0,
  85.          dist6  = 0,
  86.          dist7  = 0,
  87.          dist8  = 0,
  88.          result = 0;
  89.  
  90.       /* straight up */
  91.    measuring = 1;
  92.    i = a;
  93.    j = b;
  94.    while(measuring){
  95.       i--;
  96.       if(i >= 0){
  97.          if(the_image[i][j] == value)
  98.             dist1++;
  99.          else
  100.             measuring = 0;
  101.       }
  102.       else
  103.          measuring = 0;
  104.    }  /* ends while measuring */
  105.    result = dist1;
  106.  
  107.       /* straight down */
  108.    measuring = 1;
  109.    i = a;
  110.    j = b;
  111.    while(measuring){
  112.       i++;
  113.       if(i <= ROWS-1){
  114.          if(the_image[i][j] == value)
  115.             dist2++;
  116.          else
  117.             measuring = 0;
  118.       }
  119.       else
  120.          measuring = 0;
  121.    }  /* ends while measuring */
  122.    if(dist2 <= result)
  123.       result = dist2;
  124.  
  125.       /* straight left */
  126.    measuring = 1;
  127.    i = a;
  128.    j = b;
  129.    while(measuring){
  130.       j--;
  131.       if(j >= 0){
  132.          if(the_image[i][j] == value)
  133.             dist3++;
  134.          else
  135.             measuring = 0;
  136.       }
  137.       else
  138.          measuring = 0;
  139.    }  /* ends while measuring */
  140.    if(dist3 <= result)
  141.       result = dist3;
  142.  
  143.       /* straight right */
  144.    measuring = 1;
  145.    i = a;
  146.    j = b;
  147.    while(measuring){
  148.       j++;
  149.       if(j <= COLS-1){
  150.          if(the_image[i][j] == value)
  151.             dist4++;
  152.          else
  153.             measuring = 0;
  154.       }
  155.       else
  156.          measuring = 0;
  157.    }  /* ends while measuring */
  158.    if(dist4 <= result)
  159.       result = dist4;
  160.  
  161.       /* left and up */
  162.    measuring = 1;
  163.    i = a;
  164.    j = b;
  165.    while(measuring){
  166.       j--;
  167.       i--;
  168.       if(j >= 0 && i>=0){
  169.          if(the_image[i][j] == value)
  170.             dist5++;
  171.          else
  172.             measuring = 0;
  173.       }
  174.       else
  175.          measuring = 0;
  176.    }  /* ends while measuring */
  177.    dist5 = (dist5*14)/10;
  178.    if(dist5 <= result)
  179.       result = dist5;
  180.  
  181.       /* right and up */
  182.    measuring = 1;
  183.    i = a;
  184.    j = b;
  185.    while(measuring){
  186.       j++;
  187.       i--;
  188.       if(j <=COLS-1 && i>=0){
  189.          if(the_image[i][j] == value)
  190.             dist6++;
  191.          else
  192.             measuring = 0;
  193.       }
  194.       else
  195.          measuring = 0;
  196.    }  /* ends while measuring */
  197.    dist6 = (dist6*14)/10;
  198.    if(dist6 <= result)
  199.       result = dist6;
  200.  
  201.       /* right and down */
  202.    measuring = 1;
  203.    i = a;
  204.    j = b;
  205.    while(measuring){
  206.       j++;
  207.       i++;
  208.       if(j <=COLS-1 && i<=ROWS-1){
  209.          if(the_image[i][j] == value)
  210.             dist7++;
  211.          else
  212.             measuring = 0;
  213.       }
  214.       else
  215.          measuring = 0;
  216.    }  /* ends while measuring */
  217.    dist7 = (dist7*14)/10;
  218.    if(dist7 <= result)
  219.       result = dist7;
  220.  
  221.       /* left and down */
  222.    measuring = 1;
  223.    i = a;
  224.    j = b;
  225.    while(measuring){
  226.       j--;
  227.       i++;
  228.       if(j >=0 && i<=ROWS-1){
  229.          if(the_image[i][j] == value)
  230.             dist8++;
  231.          else
  232.             measuring = 0;
  233.       }
  234.       else
  235.          measuring = 0;
  236.    }  /* ends while measuring */
  237.    dist8 = (dist8*14)/10;
  238.    if(dist8 <= result)
  239.       result = dist8;
  240.  
  241.    return(result);
  242. }  /* ends distance_8 */
  243.  
  244.  
  245.  
  246.  
  247.  
  248.      /*******************************************
  249.      *
  250.      *   mat(..
  251.      *
  252.      *   This function finds the medial axis
  253.      *   transform for objects in an image.
  254.      *   The mat are those points that are
  255.      *   minimally distant to more than one
  256.      *   boundary point.
  257.      *
  258.      *******************************************/
  259.  
  260.  
  261. mat(in_name, out_name, the_image, out_image,
  262.     il, ie, ll, le, value)
  263.    char   in_name[], out_name[];
  264.    int    il, ie, ll, le;
  265.    short  the_image[ROWS][COLS],
  266.           out_image[ROWS][COLS],
  267.           value;
  268. {
  269.    int    a, b, count, i, j, k,
  270.           length, width;
  271.    struct tiff_header_struct image_header;
  272.  
  273.    if(does_not_exist(out_name)){
  274.       printf("\n\n output file does not exist %s", out_name);
  275.       read_tiff_header(in_name, &image_header);
  276.       round_off_image_size(&image_header,
  277.                            &length, &width);
  278.       image_header.image_length = length*ROWS;
  279.       image_header.image_width  = width*COLS;
  280.       create_allocate_tiff_file(out_name, &image_header,
  281.                                 out_image);
  282.    }  /* ends if does_not_exist */
  283.  
  284.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  285.  
  286.    for(i=0; i<ROWS; i++)
  287.       for(j=0; j<COLS; j++)
  288.          out_image[i][j] = 0;
  289.  
  290.       /***************************
  291.       *
  292.       *   Loop over image array
  293.       *
  294.       ****************************/
  295.  
  296.    printf("\n");
  297.  
  298.    for(i=0; i<ROWS; i++){
  299.       if( (i%10) == 0) printf("%3d", i);
  300.       for(j=0; j<COLS; j++){
  301.          if(the_image[i][j] == value)
  302.             out_image[i][j] = mat_d(the_image, 
  303.                                     i, j, value);
  304.       }  /* ends loop over j */
  305.    }  /* ends loop over i */
  306.  
  307.    write_array_into_tiff_image(out_name, out_image,
  308.                                il, ie, ll, le);
  309.  
  310. }  /* ends mat */
  311.  
  312.  
  313.  
  314.      /*******************************************
  315.      *
  316.      *   mat_d(..
  317.      *
  318.      *   This function helps find the medial
  319.      *   axis transform.
  320.      *
  321.      *   This function measures the distances
  322.      *   from the point to a zero pixel in all
  323.      *   eight directions.  Look for the two
  324.      *   shortest distances in the eight distances.
  325.      *   If the two shortest distances are
  326.      *   equal, then the point in question
  327.      *   is minimally distant to more than one
  328.      *   boundary point.  Therefore, it is
  329.      *   on the medial axis so return a value.
  330.      *   Otherwise, return zero.
  331.      *
  332.      *******************************************/
  333.  
  334. mat_d(the_image, a, b, value)
  335.    int   a, b;
  336.    short the_image[ROWS][COLS], value;
  337. {
  338.    int i, j, measuring;
  339.    short dist1  = 0,
  340.          dist2  = 0,
  341.          dist3  = 0,
  342.          dist4  = 0,
  343.          dist5  = 0,
  344.          dist6  = 0,
  345.          dist7  = 0,
  346.          dist8  = 0,
  347.          min1   = 255,
  348.          min2   = 255,
  349.          result = 0;
  350.  
  351.       /* straight up */
  352.    measuring = 1;
  353.    i = a;
  354.    j = b;
  355.    while(measuring){
  356.       i--;
  357.       if(i >= 0){
  358.          if(the_image[i][j] == value)
  359.             dist1++;
  360.          else
  361.             measuring = 0;
  362.       }
  363.       else
  364.          measuring = 0;
  365.    }  /* ends while measuring */
  366.    result = dist1;
  367.    min1   = dist1;
  368.  
  369.       /* straight down */
  370.    measuring = 1;
  371.    i = a;
  372.    j = b;
  373.    while(measuring){
  374.       i++;
  375.       if(i <= ROWS-1){
  376.          if(the_image[i][j] == value)
  377.             dist2++;
  378.          else
  379.             measuring = 0;
  380.       }
  381.       else
  382.          measuring = 0;
  383.    }  /* ends while measuring */
  384.    if(dist2 <= result)
  385.       result = dist2;
  386.    if(dist2 < min1){
  387.       min2 = min1;
  388.       min1 = dist2;
  389.    }
  390.    else
  391.       if(dist2 < min2)
  392.          min2 = dist2;
  393.  
  394.       /* straight left */
  395.    measuring = 1;
  396.    i = a;
  397.    j = b;
  398.    while(measuring){
  399.       j--;
  400.       if(j >= 0){
  401.          if(the_image[i][j] == value)
  402.             dist3++;
  403.          else
  404.             measuring = 0;
  405.       }
  406.       else
  407.          measuring = 0;
  408.    }  /* ends while measuring */
  409.    if(dist3 <= result)
  410.       result = dist3;
  411.    if(dist3 < min1){
  412.       min2 = min1;
  413.       min1 = dist3;
  414.    }
  415.    else
  416.       if(dist3 < min2)
  417.          min2 = dist3;
  418.       /* straight right */
  419.    measuring = 1;
  420.    i = a;
  421.    j = b;
  422.    while(measuring){
  423.       j++;
  424.       if(j <= COLS-1){
  425.          if(the_image[i][j] == value)
  426.             dist4++;
  427.          else
  428.             measuring = 0;
  429.       }
  430.       else
  431.          measuring = 0;
  432.    }  /* ends while measuring */
  433.    if(dist4 <= result)
  434.       result = dist4;
  435.    if(dist4 < min1){
  436.       min2 = min1;
  437.       min1 = dist4;
  438.    }
  439.    else
  440.       if(dist4 < min2)
  441.          min2 = dist4;
  442.  
  443.       /* left and up */
  444.    measuring = 1;
  445.    i = a;
  446.    j = b;
  447.    while(measuring){
  448.       j--;
  449.       i--;
  450.       if(j >= 0 && i>=0){
  451.          if(the_image[i][j] == value)
  452.             dist5++;
  453.          else
  454.             measuring = 0;
  455.       }
  456.       else
  457.          measuring = 0;
  458.    }  /* ends while measuring */
  459.    dist5 = ((dist5*14)+7)/10;
  460.    if(dist5 <= result)
  461.       result = dist5;
  462.    if(dist5 < min1){
  463.       min2 = min1;
  464.       min1 = dist5;
  465.    }
  466.    else
  467.       if(dist5 < min2)
  468.          min2 = dist5;
  469.  
  470.       /* right and up */
  471.    measuring = 1;
  472.    i = a;
  473.    j = b;
  474.    while(measuring){
  475.       j++;
  476.       i--;
  477.       if(j <=COLS-1 && i>=0){
  478.          if(the_image[i][j] == value)
  479.             dist6++;
  480.          else
  481.             measuring = 0;
  482.       }
  483.       else
  484.          measuring = 0;
  485.    }  /* ends while measuring */
  486.    dist6 = ((dist6*14)+7)/10;
  487.    if(dist6 <= result)
  488.       result = dist6;
  489.    if(dist6 < min1){
  490.       min2 = min1;
  491.       min1 = dist6;
  492.    }
  493.    else
  494.       if(dist6 < min2)
  495.          min2 = dist6;
  496.  
  497.       /* right and down */
  498.    measuring = 1;
  499.    i = a;
  500.    j = b;
  501.    while(measuring){
  502.       j++;
  503.       i++;
  504.       if(j <=COLS-1 && i<=ROWS-1){
  505.          if(the_image[i][j] == value)
  506.             dist7++;
  507.          else
  508.             measuring = 0;
  509.       }
  510.       else
  511.          measuring = 0;
  512.    }  /* ends while measuring */
  513.    dist7 = ((dist7*14)+7)/10;
  514.    if(dist7 <= result)
  515.       result = dist7;
  516.    if(dist7 < min1){
  517.       min2 = min1;
  518.       min1 = dist7;
  519.    }
  520.    else
  521.       if(dist7 < min2)
  522.          min2 = dist7;
  523.  
  524.       /* left and down */
  525.    measuring = 1;
  526.    i = a;
  527.    j = b;
  528.    while(measuring){
  529.       j--;
  530.       i++;
  531.       if(j >=0 && i<=ROWS-1){
  532.          if(the_image[i][j] == value)
  533.             dist8++;
  534.          else
  535.             measuring = 0;
  536.       }
  537.       else
  538.          measuring = 0;
  539.    }  /* ends while measuring */
  540.    dist8 = ((dist8*14)+7)/10;
  541.    if(dist8 <= result)
  542.       result = dist8;
  543.    if(dist8 < min1){
  544.       min2 = min1;
  545.       min1 = dist8;
  546.    }
  547.    else
  548.       if(dist8 < min2)
  549.          min2 = dist8;
  550.  
  551.    if(min1 == min2)
  552.       result = value;
  553.    else
  554.       result = 0;
  555.  
  556.    if(min1 == 0)
  557.       result = 0;
  558.  
  559.    return(result);
  560. }  /* ends mat_d */
  561.  
  562.  
  563.